<template>
  <div class="app-container">


    <el-card>

      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">数据统计</span></el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
            v-hasPermi="['wms:statistics:add']">新增</el-button>
          <el-button type="warning" plain icon="Download" @click="handleExport"
            v-hasPermi="['wms:statistics:export']">导出</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="statisticsList" border class="mt20">
        <el-table-column type="index" width="60" align="center" label="序号" fixed="left" />

        <el-table-column label="日新增数量" align="center" prop="todayQuantity" />
        <el-table-column label="周新增数量" align="center" prop="weekQuantity" />
        <el-table-column label="月新增数量" align="center" prop="monthQuantity" />
        <el-table-column label="当前信件总数" align="center" prop="total" />
        <el-table-column label="已处理数量" align="center" prop="processedTotal" />
        <el-table-column label="待处理数量" align="center" prop="pendingTotal" />
        <el-table-column label="当日处理效率" align="center" prop="efficiency" />
        <el-table-column label="当日处理数量" align="center" prop="todayProcessedTotal" />
        <el-table-column label="时间" align="center" prop="createTime" >
          <template #default="scope">
            <div>{{scope.row.createTime.substring(0, 10)||'--'}}</div>
          </template>
        </el-table-column>
      </el-table>
      <el-row>

        <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>

    </el-card>
    <!-- 添加或修改数据统计对话框 -->
    <el-drawer :title="title" v-model="open" size="40%" append-to-body>
      <el-form ref="statisticsRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="日新增数量" prop="todayQuantity">
          <el-input v-model="form.todayQuantity" placeholder="请输入日新增数量" />
        </el-form-item>
        <el-form-item label="周新增数量" prop="weekQuantity">
          <el-input v-model="form.weekQuantity" placeholder="请输入周新增数量" />
        </el-form-item>
        <el-form-item label="月新增数量" prop="monthQuantity">
          <el-input v-model="form.monthQuantity" placeholder="请输入月新增数量" />
        </el-form-item>
        <el-form-item label="当前信件总数" prop="total">
          <el-input v-model="form.total" placeholder="请输入当前信件总数" />
        </el-form-item>
        <el-form-item label="已处理数量" prop="processedTotal">
          <el-input v-model="form.processedTotal" placeholder="请输入已处理数量" />
        </el-form-item>
        <el-form-item label="待处理数量" prop="pendingTotal">
          <el-input v-model="form.pendingTotal" placeholder="请输入待处理数量" />
        </el-form-item>
        <el-form-item label="当日处理效率" prop="efficiency">
          <el-input v-model="form.efficiency" placeholder="请输入当日处理效率" />
        </el-form-item>
        <el-form-item label="当日处理数量" prop="todayProcessedTotal">
          <el-input v-model="form.todayProcessedTotal" placeholder="请输入当日处理数量" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="Statistics">
  import {
    listStatistics,
    getStatistics,
    delStatistics,
    addStatistics,
    updateStatistics
  } from "@/api/wms/statistics";

  const {
    proxy
  } = getCurrentInstance();

  const statisticsList = ref([]);
  const open = ref(false);
  const buttonLoading = ref(false);
  const loading = ref(true);
  const ids = ref([]);
  const total = ref(0);
  const title = ref("");

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      todayQuantity: undefined,
      weekQuantity: undefined,
      monthQuantity: undefined,
      total: undefined,
      processedTotal: undefined,
      pendingTotal: undefined,
      efficiency: undefined,
      todayProcessedTotal: undefined,
    },
    rules: {
      id: [{
        required: true,
        message: "ID不能为空",
        trigger: "blur"
      }],
      todayQuantity: [{
        required: true,
        message: "日新增数量不能为空",
        trigger: "blur"
      }],
      weekQuantity: [{
        required: true,
        message: "周新增数量不能为空",
        trigger: "blur"
      }],
      monthQuantity: [{
        required: true,
        message: "月新增数量不能为空",
        trigger: "blur"
      }],
      total: [{
        required: true,
        message: "当前信件总数不能为空",
        trigger: "blur"
      }],
      processedTotal: [{
        required: true,
        message: "已处理数量不能为空",
        trigger: "blur"
      }],
      pendingTotal: [{
        required: true,
        message: "待处理数量不能为空",
        trigger: "blur"
      }],
      efficiency: [{
        required: true,
        message: "当日处理效率不能为空",
        trigger: "blur"
      }],
      todayProcessedTotal: [{
        required: true,
        message: "当日处理数量不能为空",
        trigger: "blur"
      }],
    }
  });

  const {
    queryParams,
    form,
    rules
  } = toRefs(data);

  /** 查询数据统计列表 */
  function getList() {
    loading.value = true;
    listStatistics(queryParams.value).then(response => {
      statisticsList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
      id: null,
      todayQuantity: null,
      weekQuantity: null,
      monthQuantity: null,
      total: null,
      processedTotal: null,
      pendingTotal: null,
      efficiency: null,
      todayProcessedTotal: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null
    };
    proxy.resetForm("statisticsRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加数据统计";
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getStatistics(_id).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改数据统计";
    });
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["statisticsRef"].validate(valid => {
      if (valid) {
        buttonLoading.value = true;
        if (form.value.id != null) {
          updateStatistics(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          }).finally(() => {
            buttonLoading.value = false;
          });
        } else {
          addStatistics(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          }).finally(() => {
            buttonLoading.value = false;
          });
        }
      }
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除数据统计编号为"' + _ids + '"的数据项？').then(function() {
      loading.value = true;
      return delStatistics(_ids);
    }).then(() => {
      loading.value = true;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {}).finally(() => {
      loading.value = false;
    });
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('wms/statistics/export', {
      ...queryParams.value
    }, `statistics_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
